﻿using System;
using Daemon.Model;
using Daemon.Common;
using Daemon.Service.Contract;
using Microsoft.AspNetCore.Mvc;
using Daemon.Repository.Contract;
using System.Net;
using System.Net.Http;
using System.Linq;
using Microsoft.AspNetCore.Http;
namespace DaemonAPI
{
    [ApiController]
    [Route("daemonApi/[controller]/[action]")]
    public class AdminUserController : BaseApiController<AdminUser, IAdminUserRepository>
    {
        private readonly IAdminUserService _adminUserService;

        public AdminUserController(IAdminUserRepository repository,
        IHttpContextAccessor httpContextAccessor,
        IAdminUserService adminUserService) : base(repository, httpContextAccessor)
        {
            _adminUserService = adminUserService;
        }

        [HttpPost]
        [Route("")]
        public ResultModel Register([FromBody] AdminUser user)
        {
            user.AddTime = DateTime.Now;
            user.UpdateTime = DateTime.Now;
            user.Deactivated = true;
            return new ResultModel(HttpStatusCode.OK, "", _adminUserService.Register(user));
        }

        [HttpGet]
        [Route("")]
        public ResultModel ExistUserName(string userName)
        {
            var existUser = _repository.FindAll().Any(r => r.UserName == userName);
            return new ResultModel(HttpStatusCode.OK, "", existUser);
        }

        [HttpGet]
        [Route("")]
        public ResultModel ExistPhone(string phone)
        {
            var existUser = _repository.FindAll().Any(r => r.Phone == phone);
            return new ResultModel(HttpStatusCode.OK, "", existUser);
        }

        [HttpGet]
        [Route("")]
        public ResultModel ExistEmail(string email)
        {
            var existUser = _repository.FindAll().Any(r => r.Email == email);
            return new ResultModel(HttpStatusCode.OK, "", existUser);
        }
    }
}
